package com.ruoyi.dynamic.service;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.pojo.result.PageUtils;
import com.ruoyi.common.core.pojo.result.Pages;
import com.ruoyi.common.core.utils.CommonUtils;
import com.ruoyi.dynamic.mapper.RoomBanUserMapper;
import com.ruoyi.dynamic.model.pojo.entity.Room;
import com.ruoyi.dynamic.model.pojo.entity.RoomBanUser;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomAuctionStartRequest;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomBanUserSearchRequest;
import com.ruoyi.system.api.RemoteAppUserService;
import com.ruoyi.system.api.model.user.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;

/**
 * <p>
 * 房间禁言用户 服务实现类
 * </p>
 *
 * @author Gnc
 * @since 2025-01-14
 */
@Service
public class RoomBanUserServiceImpl extends ServiceImpl<RoomBanUserMapper, RoomBanUser> implements IRoomBanUserService {

    @Autowired
    private RemoteAppUserService remoteAppUserService;
    @Autowired
    @Lazy
    private IRoomService iRoomService;

    @Override
    public void addRecord(Long roomId, Long userId, Long aimUserId, Integer type) {
        Long count = lambdaQuery()
                .eq(RoomBanUser::getRoomId, roomId)
                .eq(RoomBanUser::getUserId, userId)
                .eq(RoomBanUser::getType, type)
                .count();
        Assert.isTrue(count == 0, "已在列表中");

        R<AppUser> userInfo = remoteAppUserService.getUserInfo(userId, SecurityConstants.APP);
        AppUser data = userInfo.getData();

        R<AppUser> aimUserInfo = remoteAppUserService.getUserInfo(aimUserId, SecurityConstants.APP);
        AppUser aimUserInfoData = aimUserInfo.getData();

        RoomBanUser one = new RoomBanUser();
        one.setAimUserId(aimUserId);
        one.setRoomId(roomId);
        one.setUserId(userId);
        one.setUserName(data.getNickName());
        one.setUserPhoto(data.getPhoto());
        one.setAimUserNickName(aimUserInfoData.getNickName());
        one.setCreateTime(new Date());
        one.setType(type);
        save(one);
    }

    @Override
    public boolean isBlockByUser(Long roomId, Long userId, Integer type) {
        return lambdaQuery()
                .eq(RoomBanUser::getRoomId, roomId)
                .eq(RoomBanUser::getUserId, userId)
                .eq(RoomBanUser::getType, type)
                .count() > 0;
    }

    @Override
    public Pages<RoomBanUser> banListPage(AppRoomBanUserSearchRequest request, Long userId) {
        Page<RoomBanUser> roomBanUserPage = PageUtils.initPage(request, RoomBanUser.class);
        Page<RoomBanUser> page = lambdaQuery()
                .eq(RoomBanUser::getRoomId, request.getRoomId())
                .eq(!CommonUtils.isNull(request.getType()), RoomBanUser::getType, request.getType())
                .orderByDesc(RoomBanUser::getCreateTime).page(roomBanUserPage);
        return PageUtils.pages(page);
    }

    @Override
    public void undock(AppRoomAuctionStartRequest request, Long userId) {
        Room verify = iRoomService.verify(request.getRoomId());
        Assert.isTrue(verify.getUserId().equals(userId), "您无权限操作");
        removeById(request.getId());
    }
}
